<%@ page language="java" 
         contentType="text/html; charset=windows-1256"
         pageEncoding="windows-1256"
         import="LoginUtil.*"
         import="persistent.Db4oServletListener"
         
         import="com.db4o.ObjectContainer"
         import="com.db4o.ObjectSet"
%>

 <%  User currentUser = (User) (session.getAttribute("currentSessionUser")); 
 	if(currentUser == null || currentUser.getUserType() != User.UserType.Admin)
 	{
 		response.sendRedirect("login.jsp");
 	}
 %>

   <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
   "http://www.w3.org/TR/html4/loose.dtd">

   <html>

      <head>
         <meta http-equiv="Content-Type" 
            content="text/html; charset=windows-1256">
         <title>   MedSys Patient Lookup   </title>
         <link rel="stylesheet" type="text/css" href="style.css" />
         
         <% // this script is non-functional.  It is supposed to use ajax to refresh the page %>
         <script type="text/javascript">
			function loadXMLDoc()
			{
				// create the xml http request
				var req;
				if (window.XMLHttpRequest)
				  {// code for IE7+, Firefox, Chrome, Opera, Safari
				  req=new XMLHttpRequest();
				  }
				else
				  {// code for IE6, IE5
				  req=new ActiveXObject("Microsoft.XMLHTTP");
				  }
				
				var url = "userManagementServlet";
				var params = "action=submit&firstname=" + 
				document.getElementByName("firstname").value +
				"&lastname=" +
				document.getElementByName("lastname").value +
				"&suspended=" +
				document.getElementByName("suspended").value;
				
				alert("Params: " + params);
				
				req.open("POST",url,true);    
				req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

				req.onreadystatechange=function()
				  {
				  if (req==4 && req==200)
				    {
				    document.getElementById("results").innerHTML=req.responseText;
				    }
				  else
				  {
				  	alert("Error during ajax call");
				  }
				  };
				req.send(params);
			}
		</script>
		
		<%
		ObjectContainer container = Db4oServletListener.getObjectContainer(this.getServletContext());
		ObjectSet<User> rs = container.query(User.class);
		%>
      </head>
	
      <body>

         <center>
         	<a href="adminMain.jsp">Return</a><br><br>
			<% // part of above ajax code
			// <div class="h2"><h1>Search Options</h1>
	
			// First Name:  <INPUT TYPE="text" NAME="firstnamebox" VALUE=""><br>
           	// Last Name:  <INPUT TYPE="text" NAME="lastnamebox" VALUE=""><br>
           	// Suspended?	<INPUT TYPE="checkbox" NAME="suspendedbox"><br>
           	// <button ONCLICK="loadXMLDoc()" NAME="lookupbutton">Search</button>
			
			// </div><br><br><br>
			 
			
			// <div id="results" class="h1">Search Results

			// </div>
			%>
			
			
			<FORM NAME="userManagementForm" ACTION="userManagementServlet" METHOD="GET">
			<table>
			<tr>
			<td>
				<TABLE cellpadding="5" border="1" style="foreground-color: #000000;">
				<tr>
				<th>Name</th>
				<th>User Type</th>
				<th>Username</th>
				<th>Password</th>
				<th>Suspended?</th>
				</tr>
			    <%
			    while (rs.hasNext()) {
			    	User user = rs.next();
			    %>
			    <TR>
			        <TD><%=user.getFirstName() + " " + user.getLastName()%></TD>
			        <td><%=user.getUserType() %></td>
			        <TD><%=user.getUsername()%></TD>
			        <td><%=user.getPassword() %></td>
			        <TD><%=user.isSuspended()%></TD>
			    </TR>
			    <%   }    %>
				</TABLE>
			</td>
			<td>
				<table>
				<tr>
					<td>Select an user: </td>
					<td><select name="userdrop">
							<item value="null">select an user</item>
							<%	rs.reset();
								while (rs.hasNext()){ 
							    	User curr = rs.next();%>
							        <option ><%= curr.getUsername()%></option>
							    <%}%>
						</select><br><br></td>
				</tr>
				<tr>To change a field for an user, type the new value in the relavent box.<br><br></tr>
				<tr>
					<td>New First Name:</td>
					<td><input type="text" name="firstnamebox"></td>
				</tr>
				<tr>
					<td>New Last Name:</td>
					<td><input type="text" name="lastnamebox"></td>
				</tr>
				<tr>
					<td>New Username:</td>
					<td><input type="text" name="usernamebox"></td>
				</tr>
				<tr>
					<td>New Password:</td>
					<td><input type="text" name="passwordbox"><br></td>
				</tr>
						
				<tr><td><input type="checkbox" name="suspendedcheckbox" value="suspendedcheckbox">Suspended</input></td><td /></tr>
				<tr><td><input type="checkbox" name="resetpasswordcheckbox" value="resetpasswordcheckbox">Reset password</input><br></td><td /></tr>
				<tr><td><input type="checkbox" name="deletecheckbox" value="deletecheckbox">Delete User</input><br></td><td /></tr>
						
				<tr><td /><td><button action="submit" name="submitbutton">Submit</button></td></tr>
				</table>
			</td>
			</tr>
			</table>
         </center>

      </body>
	
	
   <%
   container.close();
   %>
   </html>
   